package com.kelvin.paopao.module.main.date.datelist;

import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.DateListModel;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by MCK on 2017/5/10.
 */

public class MainDatePresenter implements IMainDateListPresenter {

    private MainDateListFragment mVIew;

    public MainDatePresenter(MainDateListFragment view) {
        this.mVIew = view;
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }

    @Override
    public void getData(boolean isRefresh, int type) {
        String order_type = null;
        if (type != 0) {
            order_type = String.valueOf(type);
        }
        RetrofitService.getDate(order_type).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                mVIew.showLoading();
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).
                observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<DateListModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<DateListModel> commonListResponse) throws Exception {
                        mVIew.hideLoading();
                        mVIew.loadData(commonListResponse.getResult());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mVIew.hideLoading();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        mVIew.hideLoading();
                    }
                });
    }
}
